package mybox.engine;

import static java.lang.Math.*;
public class QVector {
	
	public double x;
	public double y;
	String type;
	public static String lineCountPlatzhalter="lineCountPlatzhalter";
	
	public QVector (double x, double y) {
		this.x=x;
		this.y=y;
	}
	
	
	public QVector(QVector qvectorInit) {
		this.x=qvectorInit.x;
		this.y=qvectorInit.y;
	}
	
	public QVector ortho2Rechts() {
		return new QVector(y,-x);
	}
	
	public QVector ortho2Links() {
		return new QVector(-y, x);
	}
	
	public QVector normiereAuf(double requiredLength) {
		double currentLength=sqrt(x*x+y*y);
		return new QVector(requiredLength*x/currentLength,requiredLength*y/currentLength);
	}
	
	public static QVector rphi(double r, double phi) {
		QVector retVec = new QVector(0,0);
		retVec.y=r*java.lang.Math.sin(phi);
		retVec.x=r*java.lang.Math.cos(phi);
		return retVec;
	}
		
	@Override
	public String toString() {
		return "x="+x+"  y="+y;
	}
	
	public static QVector diff(QVector vec1, QVector vec2) {
		QVector diffVec = new QVector(vec1.x-vec2.x,vec1.y-vec2.y);
		return diffVec;
	}
	
	public static QVector add(QVector vec1, QVector vec2) {
		QVector addVec = new QVector(vec1.x+vec2.x,vec1.y+vec2.y);
		return addVec;
	}
	
	public QVector vecAdd(QVector vec2add) {
		QVector retVec = new QVector(this.x+vec2add.x,this.y+vec2add.y);
		return retVec;
	}
	
	public double length() {
		return java.lang.Math.sqrt((x*x)+(y*y));
	}
	
	public static double diffEuklid(QVector a, QVector b) {
		return diff(a,b).length();
	}
	
	public String printLine(QVector aktPos, boolean reallyPrint) {
		if (reallyPrint)
		 return	printLine(aktPos);
		else 
		 return noPrintLine(aktPos);			
	}
	
	private static String generateLineCode(double fromx, double fromy, double tox, double toy) {		
		return String.format("  0\nLINE" +
				"\n  5\n"+lineCountPlatzhalter+"\n100\nAcDbEntity\n100\nAcDbLine\n  8\n0\n 62\n256\n370\n-1\n  6\nByLayer"+
				"\n10\n%f\n20\n%f\n30\n0.0\n11\n%f\n21\n%f\n31\n0.0",	                           
							  fromx, fromy, tox, toy);	
	}
	
	public static String generateCircleCode(double centerX, double centerY, double radius) {						
		return String.format("  0\nCIRCLE\n" +
				"  5\n"+lineCountPlatzhalter+"\n100\nAcDbEntity\n100\nAcDbCircle\n  8\n0\n 62\n256\n370\n-1\n  6\nByLayer\n"+ 
				"10\n%f\n20\n%f\n30\n0.0\n40\n%f",
				   centerX, centerY, radius);
	}
	
	public static String printLine(QVector pos1, QVector pos2) {
		
		double fromx=pos1.x;
		double fromy=pos1.y;
		double tox=pos2.x;
		double toy=pos2.y;
		
		return generateLineCode(fromx, fromy, tox, toy);		
	}
	
	public String printLine(QVector aktPos) {
		double fromx=aktPos.x;
		double fromy=aktPos.y;
		double tox=aktPos.x+this.x;
		double toy=aktPos.y+this.y;
		aktPos.x=tox;
		aktPos.y=toy;
		
		return generateLineCode(fromx, fromy, tox, toy);
	}
	
	public String noPrintLine(QVector aktPos) {		
		double tox=aktPos.x+this.x;
		double toy=aktPos.y+this.y;
		aktPos.x=tox;
		aktPos.y=toy;
	
		return "";
	}
	

	
	public String printLineAktPosNeutral(QVector aktPos) {
		
		double fromx=aktPos.x;
		double fromy=aktPos.y;
		double tox=aktPos.x+this.x;
		double toy=aktPos.y+this.y;

		return generateLineCode(fromx, fromy, tox, toy);

	}


	


	
	
	
}
